package com.Fate.servlet;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import com.Fate.common.CodeMsg;
import com.Fate.common.Constant;
import com.Fate.common.Result;
import com.Fate.pojo.User;
import com.Fate.service.IcustomerService;
import com.Fate.service.impl.CustomerServiceImpl;
import com.Fate.util.RespUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/customer.do")
public class CustomerServlet extends HttpServlet {
    private IcustomerService customerService = new CustomerServiceImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String tag = req.getParameter("service");
        try {
            Method m = this.getClass().getDeclaredMethod(tag, HttpServletRequest.class, HttpServletResponse.class);
            m.invoke(this, req, resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 处理客户列表请求
     */
    protected void page(HttpServletRequest req, HttpServletResponse resp) {
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");
        Integer pageNo = 1;
        Integer pageSize = 10;
        if (NumberUtil.isNumber(page)) {
            pageNo = Integer.parseInt(page);
        }
        if (NumberUtil.isNumber(limit)) {
            pageSize = Integer.parseInt(limit);
        }
        //查询条件
        Map<String, String[]> parameterMap = req.getParameterMap();
        String name = req.getParameter("name");
        String sex = req.getParameter("sex");
        String phone = req.getParameter("phone");
        String company = req.getParameter("company");
        String userId = req.getParameter("userId");
        String deleted = req.getParameter("deleted");
        //查询条件容器
        Map<String, String> params = new HashMap<>();
        if (StrUtil.isNotBlank(name)) {
            params.put("name", name);
        }
        if (StrUtil.isNotBlank(sex)) {
            params.put("sex", sex);
        }
        if (StrUtil.isNotBlank(phone)) {
            params.put("phone", phone);
        }
        if (StrUtil.isNotBlank(company)) {
            params.put("company", company);
        }
        if (StrUtil.isNotBlank(userId)) {
            params.put("userId", userId);
        }
        if (StrUtil.isNotBlank(deleted)) {
            params.put("deleted", deleted);
        }

        //查询分页信息
      Result rs =   customerService.queryPage(params,pageNo,pageSize);
        //返回
        RespUtil.write(resp,rs);
    }

    /**
     * 处理新增请求
     * @param req
     * @param resp
     */
    protected void add(HttpServletRequest req,HttpServletResponse resp) throws ServletException{

        String name = req.getParameter("name");
        String phone = req.getParameter("phone");
        Integer sex =Integer.valueOf(req.getParameter("sex")) ;
        String company = req.getParameter("company");
        //获取登陆的用户
        User user = (User) req.getSession().getAttribute(Constant.CURRENT_USER_KEY);

        Result rs =customerService.add(name,phone,sex,company,user.getId());
        //将数据输出
        RespUtil.write(resp,rs);

    }

    /**
     * 修改客户数据
     */
    protected void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        String name = req.getParameter("name");
        String phone = req.getParameter("phone");
        Integer sex =Integer.valueOf(req.getParameter("sex")) ;
        String company = req.getParameter("company");

        //获取登录的用户
        User user = (User) req.getSession().getAttribute(Constant.CURRENT_USER_KEY);
        //插入数据
        Result rs = customerService.update(id,name,phone,sex,company,user.getId());
        //将数据输出
        RespUtil.write(resp,rs);
    }


    /**
     * 删除客户数据
     */
    protected void deleted(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        //插入数据
        Result rs = customerService.deleted(id);
        //将数据输出
        RespUtil.write(resp,rs);
    }

    /**
     * 退出
     */
    protected void loginout(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //删除session
        //跳转到登陆页面
        req.getSession().invalidate();
        resp.sendRedirect("index.jsp");

    }

    /**
     * 处理所有修改业务员的请求
     */
    protected void setSalesman(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //业务员id
        String userId = req.getParameter("userId");
        //客户ID
        String[] cids = req.getParameterValues("cid");
        //批量修改业务员的id
        Result rs =  customerService.batchSetSaleman(userId,cids);
        RespUtil.write(resp,rs);

    }

    /**
     * 新增拜访记录
     */
    protected void visit(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //客户id
        String id = req.getParameter("id");
        String name = req.getParameter("name");
        String visitTime = req.getParameter("visitTime");
        String descp = req.getParameter("descp");
        //获取当前登录用户的id和名字
        HttpSession session = req.getSession();
        User user = (User)session.getAttribute(Constant.CURRENT_USER_KEY);

        Integer userId = user.getId();
        String realname = user.getRealname();

        //批量修改业务员的id
        Result rs =  customerService.addVisitLog(id,name,userId,realname,visitTime,descp);
        RespUtil.write(resp,rs);

    }















































}
